// 网关配置的前缀
var blogServer = "/blogSer"
// 限制标题输入字数
var maxLength = 20;

let userId = "";
let userName = "";
var blogId = "";
var isUpdate = "";
var type = "PUT";

let Authorization = "";
let refreshToken = "";

// 加载页面时读取配置信息
window.onload = function () {
    let authorities = $("#authorities").html();
    unauthorized_2(authorities, "myBlog");

    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        blogServer = data.blogServer;
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")

    userId = $("#userId").html();
    userName = $("#userName").html();
    blogId = $("#blogId").val();
    isUpdate = $("#isUpdate").val();

    if (isUpdate === "1"){
        type = "POST";
        getBlog(blogId);
    }
}

// 限制字数
$("#title").attr({maxlength: maxLength});
$('#length-limit').html(maxLength);
//监听文章标题输入字数
$("#title").on('input propertychange', function () {
    //获取输入内容
    var userDesc = $(this).val();
    //判断字数
    var len = 0;
    if (userDesc) {
        len = userDesc.length;
    }
    //显示字数
    $("#lengthover").html((len));
});

// 提交博客
$("#submitBlog").click(function () {
    var blogTitle = $("#title").val();
    var blogContent = $("#markdown").contents().find("#customized-buttonpane").html();
    if (blogTitle === "") {
        // 博客名为空
        cocoMessage.warning(2000, "博客标题为空！", function () {
            console.log("博客标题为空！");
        });
        return;
    }

    $.ajax({
        type: type,
        async: false,
        url: blogServer + "/blog/blogInfo",
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: JSON.stringify({
            "userId": userId,
            "userName": userName,
            "blogId": blogId,
            "blogTitle": blogTitle,
            "blogContent": blogContent
        }),
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(2000, data.resp_msg, function () {
                });
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
})

// 如果是来修改博客的需要查询博客内容
function getBlog(blogId){
    $.ajax({
        type: "GET",
        async: false,
        url: blogServer + "/blog/oneBlog?blogId=" + blogId,
        headers: {
            "Content-Type": "application/json",
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        success: function (data) {
            if (data.resp_code === 0) {
                // 成功
                cocoMessage.info(1000, data.resp_msg, function () {
                });
                $("#title").val(data.datas.blogTitle);
                $("#markdown").contents().find("#customized-buttonpane").html(data.datas.blogContent);
            } else {
                // 失败
                cocoMessage.error(2000, data.resp_msg, function () {
                    console.log(data.resp_msg);
                });
            }
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText, function () {
                console.log(err);
            });
        }
    })
}
